package com.hurt.competition.player.controller;

import com.hurt.competition.admin.model.Competition;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import com.hurt.competition.common.vo.PageResult;
import com.hurt.competition.judge.dto.CompetitionJudgeReq;
import com.hurt.competition.judge.model.CompetitionJudge;
import com.hurt.competition.player.dto.CompetitionPlayerListRes;
import com.hurt.competition.player.dto.CompetitionPlayerReq;
import com.hurt.competition.player.model.CompetitionJudgePlayer;
import com.hurt.competition.player.model.CompetitionPlayer;
import com.hurt.competition.player.service.CompetitionJudgePlayerService;
import com.hurt.competition.player.service.CompetitionPlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/player/competition")
public class CompetitionPlayerController {

    @Autowired
    private CompetitionPlayerService competitionPlayerService;

    @Autowired
    private CompetitionJudgePlayerService competitionJudgePlayerService;

    @PostMapping("page")
    private CoreResult page(@RequestBody CompetitionPlayerReq req){
        PageResult<Competition> page = competitionPlayerService.page(req);
        return CoreResultBuilder.success(page);
    }

    // 查询该比赛参加的选手
    @PostMapping("list")
    private CoreResult list(@RequestBody CompetitionPlayerReq req){
        PageResult<CompetitionPlayerListRes> page = competitionPlayerService.queryList(req);
        return CoreResultBuilder.success(page);
    }

    // 查询该选手此比赛的详情
    @PostMapping("info")
    private CoreResult info(@RequestBody CompetitionPlayerReq req){
        CompetitionPlayer player = competitionPlayerService.info(req);
        return CoreResultBuilder.success(player);
    }

    // 报名比赛
    @PostMapping("/regisCompetition")
    public CoreResult regisCompetition(@RequestBody CompetitionPlayer competitionPlayer){
        if(competitionPlayer.getCompetitionId() == null || competitionPlayer.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        // 将用户信息保存到数据库中
        competitionPlayerService.regisCompetition(competitionPlayer);
        return CoreResultBuilder.success("Registration successful.");
    }

    // 审批
    @PostMapping("audit")
    public CoreResult audit(@RequestBody CompetitionPlayer competitionPlayer){
        if(competitionPlayer.getCompetitionId() == null || competitionPlayer.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        competitionPlayerService.audit(competitionPlayer);
        return CoreResultBuilder.success("成功");
    }

    // 上传作品（论文形式）
    @PostMapping("/uploadProject")
    public CoreResult uploadProject(@RequestBody CompetitionPlayer competitionPlayer) {
        if(competitionPlayer.getCompetitionId() == null || competitionPlayer.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        competitionPlayerService.uploadProject(competitionPlayer);
        return CoreResultBuilder.success("upload successful.");
    }


    // 评分
    @PostMapping("/scoring")
    public CoreResult scoring(@RequestBody CompetitionJudgePlayer competitionJudgePlayer){
        if(competitionJudgePlayer.getCompetitionId() == null || competitionJudgePlayer.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        competitionJudgePlayerService.scoring(competitionJudgePlayer);
        return CoreResultBuilder.success();
    }

    // 查看成绩
    @PostMapping("/checkScore")
    public CoreResult checkScore(@RequestBody CompetitionPlayer competitionPlayer){
        if(competitionPlayer.getCompetitionId() == null || competitionPlayer.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
            BigDecimal score = competitionPlayerService.checkScore(competitionPlayer.getUserId(),competitionPlayer.getCompetitionId());
        return CoreResultBuilder.success(score);
    }
}
